feat: surface GraphQL extensions.code on returned errors#39
Merged
Conversation
toErrorList() discarded everything but the message string from GraphQL errors, so callers had no way to distinguish error kinds (e.g. the TOTP lockout from a plain invalid-otp) without matching on message text. Attach an additive, optional `.code` from the server's extensions.code (authorizer's GraphQL layer now sets this for typed service errors) onto the returned Error objects via a new AuthorizerSDKError type. Non-breaking: existing code reading only .message is unaffected.
This was referenced Jul 10, 2026
Second-pass review found two things worth tightening: 1. "Purely additive" wasn't quite true: err.code = code adds an enumerable own property to a native Error where none existed before, so Object.keys(error) and JSON.stringify(error) change output on every error that carries a code. Switched to Object.defineProperty with enumerable: false so the field is readable but genuinely invisible to anything that enumerates or serializes the error object. 2. The array-branch conversion logic (message + extensions.code extraction) was duplicated byte-for-byte between index.ts and admin.ts's toErrorList - exactly the kind of copy that drifts when the next extensions.* field gets added to only one of them. Extracted to a shared toSDKError in a new src/errors.ts, imported by both. Added a test asserting Object.keys()/JSON.stringify() are unaffected by a present code.
Contributor
Author
|
Addressed both review findings: |
This was referenced Jul 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
toErrorList()in bothsrc/index.tsandsrc/admin.tsdiscarded everything from a GraphQL error but the message string, so callers couldn't distinguish error kinds (e.g. the new TOTP lockout in the mainauthorizerserver) without matching on message text.codefield (AuthorizerSDKError) populated from the server'sextensions.codewhen present. Non-breaking — anything reading only.messageis unaffected.Context
The
authorizerserver now setsextensions.codeon typed service errors over GraphQL (e.g.TOO_MANY_REQUESTSfor the TOTP lockout — authorizerdev/authorizer#670). This SDK change is required beforeauthorizer-reactcan key its lockout UI off a stable code instead of the raw message text.Test plan
__test__/errorCode.test.ts) with a mocked fetch, asserting.codeis populated when the server sendsextensions.codeand leftundefinedotherwisenpm test(61 tests, 5 suites, including the existing testcontainers-based integration tests)tsc --noEmitclean